package com.example.a26436.newpractise.webview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.a26436.newpractise.R;

public class WebVIiewActivity extends AppCompatActivity {
    public static final String URL = "url";

    private WebView wvWebView;
    private Button btnGoBack;
    private Button btnBackSteps;
    private Button btnGoForward;
    private Button btnForwardSteps;
    private TextView tvWebtitle;
    private ProgressBar pbGrogress;
    private String mUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        findviews();
        initdata();
        initevent();
    }

    private void findviews() {
        wvWebView = findViewById(R.id.wv_activity_web_view);
        btnGoBack = findViewById(R.id.btn_go_back_activity_web_view);
        btnBackSteps = findViewById(R.id.btn_back_steps_activity_web_view);
        btnGoForward = findViewById(R.id.btn_go_forward_activity_web_view);
        btnForwardSteps = findViewById(R.id.btn_forward_steps_activity_web_view);
        tvWebtitle = findViewById(R.id.tv_web_tile_activity_web_view);
        pbGrogress = findViewById(R.id.pb_progress_activity_web_view);

    }

    private void initdata() {
        Intent intent = getIntent();
        if (intent != null) {
            mUrl = intent.getStringExtra(URL);
        }
        wvWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                pbGrogress.setProgress(newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                tvWebtitle.setText(title);
            }
        });


        wvWebView.setWebViewClient(new WebViewClient());
        initWebSettings();

    }
     private void initWebSettings() {
         WebSettings webSettings = wvWebView.getSettings();
         webSettings.setJavaScriptEnabled(true);
         //设置自适应屏幕，两者合用
         //将图片调整到适合webview的大小
         webSettings.setUseWideViewPort(true);
         //缩放至屏幕的大小
         webSettings.setLoadWithOverviewMode(true);

         //支持缩放，默认为true。是下面那个的前提。
         webSettings.setSupportZoom(true);
         //内置缩放控件,ture,WebView可缩放;false不可缩放,并且下面的方法无效
         webSettings.setBuiltInZoomControls(true);
         //false隐藏，true显示原生的缩放控件
         webSettings.setDisplayZoomControls(true);

     }

    private void initevent() {
        if (TextUtils.isEmpty(mUrl)) {
            wvWebView.loadUrl("file:///android_asset/index.html");
        } else {
            wvWebView.loadUrl(mUrl);
        }
        btnGoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(wvWebView.canGoBack()){
                    wvWebView.goBack();
                }else{
                    ToastUtil.showToast(WebVIiewActivity.this,"决不后退");

                }

            }
        });
        btnGoForward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(wvWebView.canGoForward()){
                    wvWebView.goForward();
                }else{
                    ToastUtil.showToast(WebVIiewActivity.this,"刹住");
                }
            }
        });
        btnBackSteps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(wvWebView.canGoBackOrForward(-2)){
                    wvWebView.goBackOrForward(-2);
                }else{
                    ToastUtil.showToast(WebVIiewActivity.this,"决不绝不后退");

                }

            }
        });
        btnForwardSteps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(wvWebView.canGoBackOrForward(2)){
                    wvWebView.goBackOrForward(2);
                }else{
                    ToastUtil.showToast(WebVIiewActivity.this,"刹住刹住");

                }

            }
        });



    }

/*    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK &&wvWebView .canGoBack()){
            wvWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
        //劫持返回按键，不让Activity关闭
*//*        if(keyCode == KeyEvent.KEYCODE_BACK ){
            wvWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);*//*
    }*/
    private long mClickBackTimeMillis = 0;

       @Override
    public void onBackPressed() {
        if (wvWebView.canGoBack()){
            wvWebView.goBack();
            return;
        }else{
            long currentTimeMillis = System.currentTimeMillis();
            if(currentTimeMillis - mClickBackTimeMillis < 2000){
                super.onBackPressed();
            }else{mClickBackTimeMillis = currentTimeMillis;
                ToastUtil.showToast(WebVIiewActivity.this,"再次点击退出");

            }

        }
        //super.onBackPressed();
       //去掉就无法退出app
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        /*
        注意：实际开发中，不一定每一个方法都要执行
        主要目的：讲明白webview清除数据在哪里执行
         */
        //清除缓存数据
        wvWebView.clearCache(true);
        //清除历史记录
        wvWebView.clearHistory();
        //清除表单数据
        wvWebView.clearFormData();
    }

}
